/* -*- Mode:Prolog; coding:ISO-8859-1; -*- */
:- use_module(library(timeout)).
:- ensure_loaded([tal]).

evaluate(Solution, Evaluation) :-
        %print('\nEvaluation starting...\n'),
        get_examples(Examples),
        %print('Solution: '), portray_list(Solution), flush_output,
        theory_test_with_asserted_bg(Solution, Examples, Evaluation). 
        %print('...ending.\n').      %The bg was asserted previously (compile(File)).

get_examples(Examples) :-
        examples(Examples).     %previously asserted Examples = [example(penguin(a),-1),example(penguin(b),1)]







theory_test_with_asserted_bg([], Test, OutTest) :- !,
       test_examples_with_asserted_solution(Test, OutTest).


theory_test_with_asserted_bg([H | Solution], Test, OutTest) :- 
        assertz(H, Ref), %NOTE THE ASSERTZ, TO AVOID LOOPS
        assert(retractme(Ref), RefOfTheRef),
        %print('Asserted: '), print(Ref), flush_output,
        theory_test_with_asserted_bg(Solution, Test, OutTest),
        erase(Ref), erase(RefOfTheRef), 
        %print('Erased: '), print(Ref), flush_output,
        !. % listing(odd), listing(even).


test_examples_with_asserted_solution([], []).
test_examples_with_asserted_solution([example(E, Coeff) | T], [HOutTest | TOutTest]) :-
            %Probabilisic case: reimplement call
           bb_get_value(timeout_for_test,  X), number(X),  !, 
           (time_out(E, X, success) ->
                HOutTest = out_example(E, Coeff, 1)
            ;
                HOutTest = out_example(E, Coeff, -1)
            ),
            test_examples_with_asserted_solution(T, TOutTest).

test_examples_with_asserted_solution([example(E, Coeff) | T], [HOutTest | TOutTest]) :- 
            %Probabilisic case: reimplement call
                        
           (call(E) ->
                HOutTest = out_example(E, Coeff, 1)                 
            ;
                HOutTest = out_example(E, Coeff, -1)               
            ),
            test_examples_with_asserted_solution(T, TOutTest).

%%%%%%%%%%
check_ic(Solution, Outcome) :-
      
       theory_test_with_asserted_bgIC(Solution, Outcome).       
       

theory_test_with_asserted_bgIC([], Outcome) :- !,
       test_examples_with_asserted_solutionIC(Outcome).

theory_test_with_asserted_bgIC([H | Solution], Outcome) :- 
        assertz(H, Ref),% log(assertz(H, Ref)),
        assert(retractme(Ref), RefOfTheRef),
        theory_test_with_asserted_bgIC(Solution, Outcome),
        erase(Ref), erase(RefOfTheRef), %log(erase(Ref)), 
        !.

test_examples_with_asserted_solutionIC(Outcome) :-
           bb_get_value(timeout_for_test,  X), number(X),  !,  
           (time_out((\+(ic)), X, success) -> Outcome = succeeded; Outcome = failed).

test_examples_with_asserted_solutionIC(Outcome) :- 
        %trace,
           (call(\+(ic)) ->
                Outcome = succeeded; Outcome = failed).
                










        
get_oic(IC) :-       
        findall(Q,
                originalic((ic :- Q)),
                IC
               ).